package utils
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.controls.Alert;

	public class debug
	{
		private var fs:FileStream;
		private var opt:String;
		public function debug(opt:String)
		{
			this.opt = opt;
			switch(opt){
				case "NONE":
					break;
				case "FILE":
					var sourceFile:File = File.documentsDirectory;
					sourceFile = sourceFile.resolvePath("WebCamGolf/WebCamGolf.log");
					if (sourceFile.exists){
						if(sourceFile.creationDate.getDate() != new Date().getDate()){
							var targetFile:File = File.documentsDirectory;
							targetFile = targetFile.resolvePath("WebCamGolf/WebCamGolf." + 
								sourceFile.creationDate.getFullYear() + "-" +
								(sourceFile.creationDate.getMonth()+ 1) + "-" +
								sourceFile.creationDate.getDate());		
							sourceFile.moveTo(targetFile, true);
						}
					}
					fs = new FileStream();
					fs.open(sourceFile, FileMode.APPEND);
					break;
				case "TRACE":
					break;
				case "ALERT":
					break;
			}
		}

		public function TRACE(message:String):void
		{
			switch(opt){
				case "NONE":
					break;
				case "FILE":
					fs.writeMultiByte("Trace : ", "euckr");
					fs.writeMultiByte(new Date().toString() + "\r\n", "euckr");
					fs.writeMultiByte(message +"\r\n\r\n", "euckr");
					break;
				case "TRACE":
					trace(message);
					break;
				case "ALERT":
//					Alert.show(message, "Error");
					break;
			}

		}
		public function FATAL(e:Error = null, message:String = null):void
		{
			switch(opt){
				case "NONE":
					break;
				case "FILE":
					fs.writeMultiByte("fital Error : ", "euckr");
					fs.writeMultiByte(new Date().toString() + "\r\n", "euckr");
					fs.writeMultiByte(e.getStackTrace()+"\r\n\r\n","euckr");
					break;
				case "TRACE":
					trace(e.getStackTrace());
					break;
				case "ALERT":
					Alert.show(e.message, "Error");
					break;
			}

		} 
	}
}